package com.wing.product.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.product.model.entity.ProductItem;
import com.wing.product.model.request.ProductItemForm;
import com.wing.product.model.response.ProductItemVO;
import com.wing.product.service.ProductItemService;
import com.wing.res.model.request.TestItemData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiayang
 */
@Validated
@Api(value = "Admin-产品类型检测项管理", tags = "#/product/productItem")
@RestController("productItemController")
@RequestMapping("product/admin/productItem")
public class ProductItemController {

    @Autowired
    private ProductItemService productItemService;

    @ApiOperation(value = "查询所有产品类型检测项", notes = "搜索")
    @GetMapping("/list")
    public PageResult<ProductItemVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String name,
            Long processId,
            Long productType) {
        Page<ProductItem> pg = new Page<>(page, limit);
        Map<String,Object> paramMap = new HashMap();
        paramMap.put("sort", StrUtil.toUnderlineCase(sort));
        paramMap.put("order", StrUtil.toUnderlineCase(order));
        paramMap.put("name", name);
        paramMap.put("processId", processId);
        paramMap.put("productType", productType);
        IPage<ProductItemVO> list = productItemService.pageList4app(pg, paramMap);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "根据产品类别查询检测项", notes = "搜索-检测项")
    @GetMapping("/listByType")
    public JsonResult listByType(Long productType) {
        List<TestItemData> list = productItemService.listByType(productType);
        return JsonResult.success(list);
    }


    @ApiOperation(value = "添加产品类型检测项", notes = "添加")
    @PostMapping("/add")
    public JsonResult add(@RequestBody ProductItemForm productItemForm) {
        if (productItemService.add(productItemForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }

    @ApiOperation(value = "修改产品类型检测项", notes = "修改")
    @PostMapping("/update")
    public JsonResult update(@RequestBody ProductItemForm productItemForm) {
        ProductItem productItem = productItemService.getById(productItemForm.getId());
        if (productItem == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create().setEditable(ProductItem.class).setIgnoreError(true).setIgnoreNullValue(true);
        BeanUtil.copyProperties(productItemForm, productItem, copyOptions);
        if (productItemService.update(productItemForm) > 0) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }

    @ApiOperation(value = "删除产品类型检测项", notes = "删除")
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long productItemId) {
        if (productItemService.removeById(productItemId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "启用产品检测项", notes = "启用")
    @GetMapping("/enable")
    public JsonResult enable(Long id) {
        ProductItem productAttr = new ProductItem();
        productAttr.setId(id);
        productAttr.setIsDeleted(true);
        if (productItemService.updateById(productAttr)) {
            return JsonResult.success("启用成功");
        }
        return JsonResult.fail("启用失败");
    }

    @ApiOperation(value = "停用产品检测项", notes = "停用")
    @GetMapping("/disable")
    public JsonResult disable(Long id) {
        ProductItem productAttr = new ProductItem();
        productAttr.setId(id);
        productAttr.setIsDeleted(false);
        if (productItemService.updateById(productAttr)) {
            return JsonResult.success("停用成功");
        }
        return JsonResult.fail("停用失败");
    }

    @ApiOperation(value = "删除产品检测项", notes = "批量删除")
    @PostMapping("/delBatch")
    public JsonResult delete(String ids) {
        if (StringUtils.isNotBlank(ids)) {
            List<Long> idList = new ArrayList<>();
            String[] idArray = ids.split(",");
            for (int i = 0; i < idArray.length; i++) {
                idList.add(Long.valueOf(idArray[i]));
            }
            if (productItemService.remove(new QueryWrapper<ProductItem>().in("id", idList))) {
                return JsonResult.success("删除成功");
            }
        } else {
            return JsonResult.fail("请选择数量");
        }

        return JsonResult.fail("删除失败");
    }

    @ApiOperation(value = "查询所有产品类型检测项", notes = "查询所有产品类型检测项")
    @GetMapping("/getProductAttrByProductTypeId")
    public JsonResult<ProductItem> getProductAttrByProductTypeId(Long productTypeId) {
        QueryWrapper<ProductItem> wrapper = new QueryWrapper<>();
        wrapper.eq("product_type", productTypeId);
        List<ProductItem> productAttr = productItemService.list(wrapper);
        return JsonResult.success(productAttr);
    }
}
